﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using SPPivotViewer.HttpHandler.Internal;
using System.Net;

namespace SPPivotViewer.HttpHandler
{
    public class SPImageTileHandler : IHttpHandler
    {
        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            ImageRequest request = new ImageRequest(context.Request.Url);

            Collection collection = CollectionCache.Instance.Get(request.DzcName);
            if (null == collection)
            {
                //TODO: Draw this message onto an image tile so it can be seen in Pivot.

                context.Response.StatusCode = (int)HttpStatusCode.NotFound;
                context.Response.StatusDescription = "Pivot image not found. Cache may have expired.";
                return;
            }

            CollectionImageTileBuilder builder = new CollectionImageTileBuilder(collection, request,
                DzcSerializer.DefaultMaxLevel, DzcSerializer.DefaultTileDimension);
            builder.Write(context.Response);
        }
    }
}
